package org.jspresso.framework.application.backend.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.jspresso.framework.action.IAction;
import org.jspresso.framework.action.IActionHandler;
import org.jspresso.framework.application.frontend.action.FrontendAction;
import org.jspresso.model.TestEntity;

public class RemoveTestEntityCollectionFrontAction<E,F,G> extends FrontendAction<E,F,G> {
	
	IAction deleteCollectionFromMasterAction;
	IAction removeCollectionFromMasterAction;
	
	@Override
	public boolean execute(IActionHandler actionHandler, Map<String, Object> context) {
		List<TestEntity> testEntitiesToRemove = getSelectedModels(context);
		List<TestEntity> testEntityCollection = getModel(context);
		
		int indicesToDelete[] = {};
		int indicesToRemove[] = {};
		
		
		for(TestEntity testEntityToRemove : testEntitiesToRemove) {
			if(testEntityToRemove.isGeneratedEntity()) {
				indicesToDelete = ArrayUtils.add(indicesToDelete, testEntityCollection.indexOf(testEntityToRemove));
			}
		}
		
		setSelectedIndices(indicesToDelete, context);
		actionHandler.execute(deleteCollectionFromMasterAction, context);
		
		for(TestEntity testEntityToRemove : testEntitiesToRemove) {
			if(!testEntityToRemove.isGeneratedEntity()) {
				indicesToRemove = ArrayUtils.add(indicesToRemove, testEntityCollection.indexOf(testEntityToRemove));
			}
		}
		
		setSelectedIndices(indicesToRemove, context);
		actionHandler.execute(removeCollectionFromMasterAction, context);
		
		return super.execute(actionHandler, context);
	}
	
	
	
	public void setDeleteCollectionFromMasterAction(IAction deleteCollectionFromMasterAction) {
		this.deleteCollectionFromMasterAction = deleteCollectionFromMasterAction;
	}
	public void setRemoveCollectionFromMasterAction(IAction removeCollectionFromMasterAction) {
		this.removeCollectionFromMasterAction = removeCollectionFromMasterAction;
	}
	
	

}
